package com.java1234.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

import com.java1234.dao.IBorrowerDao;
import com.java1234.dao.IUserDao;
import com.java1234.dao.impl.BorrowerImpl;
import com.java1234.dao.impl.UserImpl;
import com.java1234.model.Borrower;
import com.java1234.model.User;
import com.java1234.util.DbUtil;
import com.java1234.util.StringUtil;

public class BorrowerAddInterFrm extends JInternalFrame {
	private JTextField borrowerAccountTxt;
	private JTextField borrowerNameTxt;

	private DbUtil dbUtil = new DbUtil();
	private IBorrowerDao borrowerDao = new BorrowerImpl();
	private IUserDao userDao = new UserImpl();
	private JTextField passwordTxt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BorrowerAddInterFrm frame = new BorrowerAddInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BorrowerAddInterFrm() {
		setTitle("借阅者添加");
		setIconifiable(true);
		setClosable(true);
		// 改变系统默认字体
		Font font = new Font("Dialog", Font.PLAIN, 12);
		java.util.Enumeration keys = UIManager.getDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof javax.swing.plaf.FontUIResource) {
				UIManager.put(key, font);
			}
		}
		setBounds(100, 100, 856, 724);

		JLabel lblNewLabel_1 = new JLabel("借阅者账号：");

		JLabel lblNewLabel_2 = new JLabel("借阅者姓名：");

		borrowerAccountTxt = new JTextField();
		borrowerAccountTxt.setColumns(10);

		borrowerNameTxt = new JTextField();
		borrowerNameTxt.setColumns(10);

		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				borrowerAddActionPerformed(e);
			}
		});

		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});

		JLabel lblNewLabel = new JLabel("密码：");

		passwordTxt = new JTextField();
		passwordTxt.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup()
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup().addGap(197).addComponent(lblNewLabel_1).addGap(11)
								.addComponent(borrowerAccountTxt, GroupLayout.PREFERRED_SIZE, 270,
										GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup().addGap(195).addComponent(lblNewLabel_2).addGap(13)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
										.addComponent(passwordTxt).addComponent(borrowerNameTxt,
												GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))))
						.addGroup(groupLayout.createSequentialGroup().addGap(201)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addGroup(groupLayout.createSequentialGroup().addComponent(btnNewButton)
												.addGap(94).addComponent(btnNewButton_1))
										.addComponent(lblNewLabel))))
				.addContainerGap(270, Short.MAX_VALUE)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addContainerGap(192, Short.MAX_VALUE)
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
										.addGroup(groupLayout.createSequentialGroup().addComponent(lblNewLabel_1)
												.addGap(64).addComponent(lblNewLabel_2).addGap(86))
										.addGroup(
												groupLayout.createSequentialGroup()
														.addComponent(borrowerAccountTxt, GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
														.addGap(63)
														.addComponent(borrowerNameTxt, GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
														.addGap(78)))
								.addGap(73))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel)
										.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
								.addGap(70)))
				.addGap(11).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
				.addGap(196)));
		getContentPane().setLayout(groupLayout);

	}

	/**
	 * 借阅者添加事件处理
	 * 
	 * @param e
	 */
	private void borrowerAddActionPerformed(ActionEvent evt) {
		String borrowerAccount = this.borrowerAccountTxt.getText();
		String borrowerName = this.borrowerNameTxt.getText();
		String password = this.passwordTxt.getText();
		if (StringUtil.isEmpty(borrowerName)) {
			JOptionPane.showMessageDialog(null, "借阅者姓名不能为空！");
			return;
		}
		if (StringUtil.isEmpty(borrowerAccount)) {
			JOptionPane.showMessageDialog(null, "借阅者账号不能为空！");
			return;
		}
		Borrower borrower = new Borrower(borrowerAccount, borrowerName, password);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			int m = borrowerDao.add(con, borrower);
			int n = userDao.addBorrower(con, borrower);
			if (m == 1 && n == 1) {
				JOptionPane.showMessageDialog(null, "借阅者添加成功！");
				resetValue();
			} else {
				JOptionPane.showMessageDialog(null, "借阅者添加失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "借阅者添加失败！");
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 重置事件处理
	 * 
	 * @param e
	 */
	private void resetValueActionPerformed(ActionEvent e) {
		this.resetValue();

	}

	/**
	 * 重置表单
	 */
	private void resetValue() {
		this.borrowerAccountTxt.setText("");
		this.borrowerNameTxt.setText("");
		this.passwordTxt.setText("");
	}
}
